public class SnakeHead extends SnakePixel {
    // 方向键定义
    static public final char UP = 'w';
    static public final char DOWN = 's';
    static public final char LEFT = 'a';
    static public final char RIGHT = 'd';

    private char dirent = RIGHT;
    public static Canvas canvas = null;

    public SnakeHead() {
        super(3, 3, SnakeColor.RED, '@');
    }
    public SnakeHead(Canvas canvas) {
        this(canvas, 3, 3, SnakeColor.RED, '@', RIGHT);
    }

    public SnakeHead(Canvas canvas, int x, int y) {
        this(canvas, x, y, SnakeColor.RED, '@', RIGHT);
    }

    public SnakeHead(Canvas canvas, int x, int y, int color, char ch, char dirent) {
        super(x, y, color, ch);
        this.canvas = canvas;
        this.dirent = dirent;
    }

    public SnakeHead set(int x, int y, int color, char ch, char dirent) {
        if (canvas == null || (canvas != null && canvas.check(x, y)))
            super.set(x, y, color, ch);
        this.dirent = dirent;

        return this;
    }

    public SnakeHead set(int color, char ch) {
        this.color = color;
        this.ch = ch;
        return this;
    }

    public Pos getDirInc() {
        Pos inc = new Pos();

        switch (dirent) {
            case UP:
                inc.set(1, 0);
                break;
            case DOWN:
                inc.set(-1, 0);
                break;
            case LEFT:
                inc.set(0, 1);
                break;
            case RIGHT:
                inc.set(0, -1);
                break;
            default:
                break;
        }
        return inc;
    }

    public SnakeHead setDir(char uDir) {
        if (!(uDir == UP || uDir == DOWN || uDir == LEFT || uDir == RIGHT))
            return this;

        if ((dirent == UP && uDir == DOWN)
                || (dirent == DOWN && uDir == UP)
                || (dirent == LEFT && uDir == RIGHT)
                || (dirent == RIGHT && uDir == LEFT)) {
            return this;
        }

        dirent = uDir;

        return this;
    }

    public SnakeHead move() {
        switch (dirent) {
            case UP:
                x--;
                break;
            case DOWN:
                x++;
                break;
            case LEFT:
                y--;
                break;
            case RIGHT:
                y++;
                break;
            default:
                break;
        }
        return this;
    }
}
